<!--
  ~ Copyright 2022 Red Hat, Inc. and/or its affiliates.
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~       http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

{#if info:isDataIndexAvailable}
<a href="{urlbase}/workflowInstances" class="badge badge-light">
    <i class="fa fa-project-diagram fa-fw"></i>
    Workflow Instances <span id="workflowInstancesCount" class="badge badge-light"/>
</a>
<a href="{urlbase}/monitoring" class="badge badge-light">
    <i class="fa fa-tachometer fa-fw"></i>
    Monitoring
</a>
{/if}
{#if info:isDataIndexAvailable == false}
<p>No Kogito addons detected.</p>
{/if}
{#if info:isDataIndexAvailable}
<script>
    const graphqlUrl = "{config:property('kogito.data-index.url') ?: ''}/graphql";

    {|
        const fillCount = () => {
            fetch(graphqlUrl, {
                method: "POST",
                headers: {
                    "Content-Type": "application/json",
                    Accept: "application/json",
                },
                body: JSON.stringify({query: "query getAllProcessesIds{  ProcessInstances{ id } }"}),
            })
                .then(r => r.json())
                .then(json => $("#workflowInstancesCount").text(json.data.ProcessInstances.length))
                .catch(error => {
                    console.log("Error while trying to fetch workflow instances count.");
                    console.error(error);
                });
        };

        fillCount();
    |}
</script>
{/if}
